package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.exceptions.DmException;
import com.hdax.dm.exceptions.constant.ErrConstant;
import com.hdax.dm.mappers.UserMapper;
import com.hdax.dm.service.UserService;
import com.hdax.dm.user.User;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    /**
     * 账号密码登录验证
     * @param phone
     * @param password
     * @return
     */
    @Override
    public List<User> loginUser(String phone, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        wrapper.eq("password",password);

        List<User> users = baseMapper.selectList(wrapper);
        if (users!=null && users.size()>0){
            return users;
        }else {
            throw new DmException(ErrConstant.ErrCodeConstant.ERR_USER_NUMBER, ErrConstant.ErrMsgConstant.ERR_USER_NUMBER);
        }

    }
    /**
     * 手机号登录验证
     */
    @Override
    public List<User> loginUser(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);

        List<User> users = baseMapper.selectList(wrapper);
        if (users!=null && users.size()>0){
            return users;
        }else {
            throw new  DmException(ErrConstant.ErrCodeConstant.ERR_USER_NUMBER, ErrConstant.ErrMsgConstant.ERR_USER_NUMBER);
        }
    }

    /**
     * 手机号注册
     * @return
     */
    @Override
    public int register(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",user.getPhone());
        List<User> users = baseMapper.selectList(wrapper);
        if (users!=null &users.size()>0){
            throw new DmException(ErrConstant.ErrCodeConstant.ERR_PHONE_NUMBER,ErrConstant.ErrMsgConstant.ERR_PHONE_NUMBER);
        }

        return baseMapper.insert(user);
    }


}
